package com.gxa.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.business.entity.Field;
import com.gxa.business.mapper.FieldMapper;
import com.gxa.business.service.FieldService;
import com.gxa.common.base.RespResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Service
@Slf4j
public class FieldServiceImpl implements FieldService {

    @Resource
    private FieldMapper fieldMapper;

    @Override
    public RespResult createFiledInfo(String name, Byte status) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        Field field = new Field(name,timestamp,status);
        fieldMapper.insert(field);
        return RespResult.success("新建领域列表成功！");
    }

    @Override
    public RespResult editFiledInfo(String id, String name, Byte status) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        UpdateWrapper<Field> fieldUpdateWrapper = new UpdateWrapper<>();
        fieldUpdateWrapper.eq("id",id).set("name",name).set("create_time",timestamp).set("status",status);
        fieldMapper.update(null,fieldUpdateWrapper);
        return RespResult.success("编辑领域列表成功！");
    }

    @Override
    public RespResult updateStatus(String id) {
        fieldMapper.deleteById(id);
        return RespResult.success("下架成功！");
    }

    @Override
    public RespResult queryAllFiledInfo(int currentPage, int pageSize) {
        Page<Field> fieldPage = new Page<>(currentPage,pageSize);
        IPage<Field> fieldIPage = fieldMapper.selectPage(fieldPage,null);
        return RespResult.success(fieldIPage.getRecords());
    }
}
